﻿//------------------------------------------------------------------------------
// ╭─────────────────────────────╮
// │ ╭─╮     ╭─╮              TM │   ╠═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╣
// │ │ │     │ │                 │                                           ☺  
// │ │ ╰───╮ │ │ ╭─────╮ ╭─────╮ │     Name:Wedn.Net HtmlHelper扩展方法(模版)类 ♮ ♪ ♩
// │ │ ╭─╮ │ │ │ │ ╭─╮ │ │ ╭─╮ │ │                                     ♫ ♭      
// │ │ ╰─╯ │ │ │ │ ╰─╯ │ │ ╰─╯ │ │     Author:iceStone                ♬ ♪       
// │ └─────╯ └─╯ ╰─────╯ ╰───╮ │ │     Chinese:汪磊                              
// │                     ┌───╯ │ │                                              
// │                     ╰─────╯ │   ╠═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╣
// ╰─────────────────────────────╯                                              
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Wedn.Net.Common;
using Wedn.Net.Model;
using Wedn.Net.Utility;

namespace Wedn.Net.Extensions
{
    /// <summary>
    /// HtmlHelper扩展方法(模版)类
    /// </summary>
    public static class Template
    {
        #region Old
        ///// <summary>
        ///// 获取页面执行数据库查询次数
        ///// </summary>
        ///// <param name="htmlHelper"></param>
        ///// <returns>次数</returns>
        //public static int GetQueries(this HtmlHelper htmlHelper)
        //{
        //    int count = Statistics.Instance.QueryCount;
        //    return count;
        //}
        ///// <summary>
        ///// 获取页面执行时间
        ///// </summary>
        ///// <param name="htmlHelper"></param>
        ///// <returns></returns>
        //public static string GetExcuteTime(this HtmlHelper htmlHelper)
        //{
        //    return Statistics.Instance.ExcuteTime.TotalSeconds.ToString(CultureInfo.InvariantCulture);
        //}

        //#region 渲染菜单导航 +static MvcHtmlString RenderMenuNav(this HtmlHelper html, string ulId = "nav", string ulClass = "menu", string subUlClass = "sub-menu", string itemClass = "menu-item")
        ///// <summary>
        ///// 渲染菜单导航
        ///// </summary>
        ///// <param name="html">HtmlHelper</param>
        ///// <param name="ulId">总UL ID</param>
        ///// <param name="ulClass">总UL 类名</param>
        ///// <param name="subUlClass">子UL 类名</param>
        ///// <param name="itemClass">单项类名</param>
        ///// <returns>菜单导航Html</returns>
        //public static MvcHtmlString RenderMenuNav(this HtmlHelper html, string ulId = "nav", string ulClass = "menu", string subUlClass = "sub-menu", string itemClass = "menu-item")
        //{
        //    var navs = BLLFactory.BLLFactory.Instance.LinkManager.Select(l => l.Type == (short)LinkType.Navigation).OrderBy(l => l.Order).ToList();
        //    var currentNav = navs.FirstOrDefault(n => String.Equals(GetHttpUrl(n.Url), HttpContext.Current.Request.Url.ToString(), StringComparison.CurrentCultureIgnoreCase));
        //    var sbHtml = new StringBuilder(string.Format("<ul id=\"{0}\" class=\"{1}\">\r\n", ulId, ulClass));
        //    foreach (var item in navs.Where(l => l.ParentId == 0))
        //    {
        //        sbHtml.AppendFormat("\t<li id=\"menu_item_{0}\" class=\"{1} menu-item-{0}\">\r\n\t\t<a href=\"{2}\" title=\"{3}\" target=\"{4}\" rel=\"{5}\">{6}</a>\r\n",
        //            item.Id, currentNav != null && currentNav.Id == item.Id ? string.Format("current-{0} {0}", itemClass) : itemClass, item.Url, item.Title, item.Target, item.Rel, item.Text);
        //        if (currentNav != null) LoadSubMenuNav(navs, currentNav.Id, item.Id, sbHtml, subUlClass, itemClass);
        //        sbHtml.Append("\t</li>\r\n");
        //    }
        //    sbHtml.Append("\t</ul>\r\n");
        //    return new MvcHtmlString(sbHtml.ToString());
        //}
        //private static void LoadSubMenuNav(IEnumerable<Link> navs, int currentNavId, int parentId, StringBuilder sbHtml, string subUlClass, string itemClass)
        //{
        //    var childs = navs.Where(l => l.ParentId == parentId);
        //    if (!childs.Any()) return;
        //    sbHtml.AppendFormat("\t\t<ul class=\"{0}\">\r\n", subUlClass);
        //    foreach (var item in childs)
        //    {
        //        sbHtml.AppendFormat("\t\t\t<li id=\"menu_item_{0}\" class=\"{1} menu-item-{0}\">\r\n\t\t\t\t<a href=\"{2}\" title=\"{3}\" target=\"{4}\" rel=\"{5}\">{6}</a>\r\n",
        //            item.Id, currentNavId == item.Id ? string.Format("current-{0} {0}", itemClass) : itemClass, item.Url, item.Title, item.Target, item.Rel, item.Text);
        //        LoadSubMenuNav(navs, currentNavId, item.Id, sbHtml, subUlClass, itemClass);
        //        sbHtml.Append("\t\t\t</li>\r\n");
        //    }
        //    sbHtml.Append("\t\t</ul>");
        //}
        //private static string GetHttpUrl(string url)
        //{
        //    if (url.IndexOf("://", StringComparison.Ordinal) == -1)
        //        url = Config.SiteUrl + url.TrimStart('/');
        //    return url;
        //}
        //#endregion

        ///// <summary>
        ///// 获取Gravatar头像地址
        ///// </summary>
        ///// <param name="htmlHelper"></param>
        ///// <param name="email">邮箱地址</param>
        ///// <param name="def">缺省头像地址</param>
        ///// <param name="size">大小</param>
        ///// <param name="rank">等级</param>
        ///// <returns>Gravatar头像地址</returns>
        //public static string GetGravatar(this HtmlHelper htmlHelper, string email, string def, int size, string rank)
        //{
        //    return GravatarHelper.GetGravatar(email, def, size, rank);
        //}

        /////// <summary>
        /////// 取上一篇文章A连接标签
        /////// </summary>
        /////// <param name="htmlHelper"></param>
        /////// <param name="current">当前文章</param>
        /////// <param name="format">格式</param>
        /////// <returns>上一篇文章A连接标签</returns>
        ////public static MvcHtmlString GetPreviousPostLink(this HtmlHelper htmlHelper, Post current, string format)
        ////{
        ////    var prev = BLLFactory.BLLFactory.Instance.PostManager.SelectSingle(p => (p.Status == (short)PostStatus.Pubilc || p.Status == (short)PostStatus.Encrypt) && p.PubDate <= current.PubDate && p.Id != current.Id);
        ////    if (prev == null)
        ////    {
        ////        return new MvcHtmlString(string.Format(format, "没有了"));
        ////    }
        ////    prev.Link = .Post(prev.Slug);
        ////    string aLink = string.Format("<a href=\"{0}\" rel=\"prev\">{1}</a>", prev.Link, prev.Title);
        ////    return new MvcHtmlString(string.Format(format, aLink));
        ////}

        /////// <summary>
        /////// 取下一篇文章A连接标签
        /////// </summary>
        /////// <param name="htmlHelper"></param>
        /////// <param name="current">当前文章</param>
        /////// <param name="format">格式</param>
        /////// <returns>下一篇文章A连接标签</returns>
        ////public static MvcHtmlString GetNextPostLink(this HtmlHelper htmlHelper, Post current, string format)
        ////{
        ////    var next = BLLFactory.BLLFactory.Instance.PostManager.SelectSingle(p => (p.Status == (short)PostStatus.Pubilc || p.Status == (short)PostStatus.Encrypt) && p.PubDate >= current.PubDate && p.Id != current.Id);
        ////    if (next == null)
        ////    {
        ////        return new MvcHtmlString(string.Format(format, "没有了"));
        ////    }
        ////    next.Link = Url.Post(next.Slug);
        ////    string aLink = string.Format("<a href=\"{0}\" rel=\"prev\">{1}</a>", next.Link, next.Title);
        ////    return new MvcHtmlString(string.Format(format, aLink));
        ////}

        ///// <summary>
        ///// 渲染原系统底部HTML
        ///// </summary>
        ///// <param name="htmlHelper"></param>
        ///// <returns>底部HTML</returns>
        //public static MvcHtmlString RenderFooter(this HtmlHelper htmlHelper)
        //{
        //    if (BLLFactory.BLLFactory.Instance.UserManager.CheckLogin())
        //    {
        //        var currentUser = SessionHelper.GetSession<User>("CurrentUser");
        //        //return new MvcHtmlString("<link type=\"text/css\" href=\"/content/styles/admin-bar.css\" rel=\"stylesheet\" /><div class=\"w-navbar w-navbar-fixed-top\"><a class=\"brand\" href=\"/Admin\">● WEDN.NET</a><span class=\"login-info\">${user_name}</span></div>".Replace("${user_name}", currentUser.NickName));
        //        return new MvcHtmlString(System.IO.File.ReadAllText(HttpContext.Current.Server.MapPath("/Views/Shared/admin-bar.html"), Encoding.UTF8).Replace("${user_name}", currentUser.NickName));
        //    }
        //    return new MvcHtmlString(string.Empty);
        //}
        ///// <summary>
        ///// 用户是否已登陆
        ///// </summary>
        //public static bool IsLogin(this HtmlHelper htmlHelper)
        //{
        //    return BLLFactory.BLLFactory.Instance.UserManager.CheckLogin();
        //} 
        #endregion
    }
}
